$(function() {
	var jadeCompile_root = null;
	var jadeCompile_folder = null;
	var jadeCompile_url = null;
	var bookmarksJson = null;

	function parse_urls(array) {
		return jadeCompile_url(array);
	}

	function parse_folders(object) {
		var html = jadeCompile_folder(object);

		return html;
	}

	function parse_folders(object) {
		var html = jadeCompile_folder(object);
		$(html).find(".folders").html();
		$(html).find(".urls").html();
		return html;
	}

	function parse_root (root) 
		var html = jadeCompile_root(root);
		return html;
	}
	function parse(rootObject) {
		var html = "";
		$.each(rootObject.roots, function(i, field) {
			filld.name
			html += jadeCompile_folder(field);
			// parse_object(field);
		});

		// var html = jadeCompile_root(rootObject);
		html = parse_root(rootObject);
		$('#contents-folder').html(html);

		$(".folder>div").click(function() {
			var div_folder = $(this).parent();
			var div_children = $(div_folder).children(".children");
			if(div_children.hasClass("has-contents")) {
				div_children.toggle();
				$(this).children("img#closed").toogle();
				$(this).children("img#open").toogle();

			} else {
				div_children.addClass("has-contents");
			}
			// folder.html();
		})
	}

	function unfold(argument) {
		// body...
	}

	$.get("root.jade", function(jade_text) {
		jadeCompile_root = jade.compile(jade_text);
		$.get("folder.jade", function(jade_text) {
			jadeCompile_folder = jade.compile(jade_text);
			$.get("urls.jade", function(jade_text) {
				jadeCompile_url = jade.compile(jade_text);
				$.getJSON("Bookmarks.txt", function(json) {
					bookmarksJson = json;
					// var html = fn(json);
					// $('#contents-folder').html(html);
					parse(json);
				});
			});
		});
	});
});
